import urllib.request
import re
from MatchDirectory.Model  import *
import sqlite3
import sqlitehelper
import fileHelper
import config
import bs4
import bs4.element

# BOOK_UNIT_LINK_PATTERN = '''<div class="bgg"><a href='(.*?)'>(.*?)</a></div>'''


def book_info(bookid):
    #http://m.qu.la/book/14/
    url = "{host}/book/{bookid}/".format(host=config.host_url,bookid=bookid)
    req = urllib.request.Request(url)
    html = urllib.request.urlopen(req)
    doc = html.read().decode('gbk', 'ignore')
    # print(doc)
    bf = bs4.BeautifulSoup(doc,"html.parser")
    # print(bf.find('div','lb_fm').table)
    # print(.find('img').attrs['src'])
    info_table = bf.find('div','lb_fm').table
    imgurl = info_table.find('img').attrs['src']
    book_name_tag = info_table.find('div',style=True)
    book_name = book_name_tag.get_text()
    book_name_tag.extract()

    last_update_content_url_tag = info_table.find('a',style=True,href=True)
    last_update_content_url = last_update_content_url_tag.attrs['href']

    last_update_content_id = re.findall('/(-?[1-9]\d*)\.html',last_update_content_url_tag.attrs['href'])[0]

    last_update_content_tag = info_table.find('div',style=True)
    last_update_content = last_update_content_tag.get_text()
    last_update_content_tag.extract()

    tag_list = info_table.find_all('div')
    auth = tag_list[0].get_text()
    last_update = tag_list[1].get_text()

    book = Book()

    book.id = bookid
    book.name = book_name
    book.auth = auth
    book.imgurl = imgurl
    book.last_update = last_update
    book.last_update_content = last_update_content
    book.last_update_content_id = last_update_content_id
    book.last_update_content_url = last_update_content_url

    Book.created_table()

    res = book.save_to_db()
    if res.rowcount > 0:
        print("ok~{}".format(bookid))
        return book
    print("no~{}".format(bookid))
    return None






def match_directory(bookid):
    BOOK_UNIT_LINK_PATTERN = '''<div class="bgg"><a href='(/book/(.*?)/(.*?).html)'>(.*?)</a></div>'''
    url = config.host_url+'/booklist/{}.html'.format(bookid)
    req = urllib.request.Request(url)
    html = urllib.request.urlopen(req)
    doc = html.read().decode('gbk', 'ignore')
    alist = list(set(re.findall(BOOK_UNIT_LINK_PATTERN, doc)))
    if len(alist) <= 0:
        print(doc)
        print('------')
        print(html.read().decode('utf8', 'ignore'))
        exit()
    else:
        cx = sqlite3.connect(sqlitehelper.SQLITE_FILE)
        cu = cx.cursor()
        sql = BookUnit.sql_created_table()
        try:
            print("sql => {}".format(sql))
            cu.execute(sql)
            print("sql exec complete~")
            cx.commit()
        except Exception as err:
            print("创建表失败.." + sql)
            cx.rollback()
            cx.close()
            exit()

        modelList = list()
        for item in alist:
            # print(item)
            bookUnit = BookUnit(id=item[2], bookid=item[1], title=item[3], url=item[0])
            modelList.append(bookUnit)

        modelListNoM = list()
        modelList_ID = list()

        for unit in modelList:
            flag = 0
            for id in modelList_ID:
                if id == unit.id:
                    print("发现重复值")
                    fileHelper.write(sqlitehelper.SQLITE_LOG_FILE, "=========发现重复值==========")
                    fileHelper.write(sqlitehelper.SQLITE_LOG_FILE, unit.sql_insert())
                    index = modelList_ID.index(id)
                    unitTemp = modelListNoM[index]
                    fileHelper.write(sqlitehelper.SQLITE_LOG_FILE, unitTemp.sql_insert())

                    fileHelper.write(sqlitehelper.SQLITE_LOG_FILE, "===================")

                    flag = 1
                else:
                    pass
            if flag == 0:
                modelListNoM.append(unit)
                modelList_ID.append(unit.id)
            flag = 0

        try:
            for unit in modelListNoM:
                sql = unit.sql_insert()
                # print("sql => {}".format(sql))
                cu.execute(sql)
                # print("sql exec complete~")
            cx.commit()

        except Exception as err:
            cx.rollback()
            print("执行操作失败..{}".format(err))
        finally:
            cx.close()
            # exit()


if __name__ == '__main__':
    #http://m.qu.la/booklist/14.html
    # match_directory(14)
    book_info(14)
    book_info(4687)

